package com.hancher.gamelife2.splash;

import android.os.Bundle;

import androidx.navigation.Navigation;

import com.hancher.common.androidutil.AsyncUtils;
import com.hancher.common.androidutil.MmkvUtil;
import com.hancher.common.base.BaseApplication;
import com.hancher.common.base.BaseMvpFragment;
import com.hancher.gamelife2.R;
import com.hancher.gamelife2.databinding.FragmentSplashBinding;
import com.hancher.gamelife2.function.FunctionPresenter;
import com.hancher.gamelife2.login.BaiduLoginFragment;
import com.kongzue.dialogx.dialogs.MessageDialog;
import com.lxj.xpopup.XPopup;

/**
 * 作者 : Hancher ytu_shaoweijie@163.com <br/>
 * 时间 : 2023/3/12 0012 22:49 <br/>
 * 描述 :
 */
public class SplashFragment extends BaseMvpFragment<FragmentSplashBinding, FunctionPresenter> {


    private String IS_AGGREE_PROTOCOL = "IS_AGGREE_PROTOCOL";
    private MessageDialog dialog;

    @Override
    protected void initView(Bundle savedInstanceState) {

    }

    @Override
    protected void initListener() {
    }

    @Override
    protected void initData() {
    }

    @Override
    public void onResume() {
        super.onResume();
        if (dialog != null && dialog.isShow()) {
            return;
        }
        boolean isAgree = MmkvUtil.getBoolean(IS_AGGREE_PROTOCOL, false);
        if (isAgree) {
            checkBaiduToken();
            return;
        }
        AsyncUtils.runCountdownOnce(100L, () ->
                dialog = MessageDialog.show("游戏人生用户隐私条款", getResources().getString(R.string.dialog_protocol), "同意", "取消")
                        .setOkButtonClickListener((dialog, v) -> {
                            MmkvUtil.put(IS_AGGREE_PROTOCOL, true);
                            checkBaiduToken();
                            dialog.dismiss();
                            return false;
                        })
                        .setCancelButtonClickListener((dialog, v) -> {
                            BaseApplication.finish();
                            return false;
                        }));

    }

    private void checkBaiduToken() {
        // 检查百度网盘权限
        long expireTime = MmkvUtil.getLong(BaiduLoginFragment.BAIDUNET_EXPIRATION_TIME, 0L);
        if (System.currentTimeMillis() > expireTime) {
            new XPopup.Builder(getContext()).asConfirm("数据备份到百度网盘", "本人太穷，没有钱购买服务器，因此利用百度网盘备份数据，点击确定进行登录百度网盘账号，谢谢理解。",
                            () -> Navigation.findNavController(binding.splashBg).navigate(R.id.action_navigation_splash_to_baiduLoginFragment))
                    .show();
            return;
        }
        Navigation.findNavController(binding.splashBg).navigate(R.id.action_navigation_splash_to_mainFragment);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (dialog != null && dialog.isShow()) {
            dialog.dismiss();
        }
    }
}